package cn.declare.controller;

import cn.declare.bean.Product;
import cn.declare.bean.Producttype;
import cn.declare.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * author：皇甫
 */
@RestController("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 查询
     * @param pageIndex
     * @param productTypeName
     * @return
     */
    @RequestMapping("/getProductList")
    public List<Product> getProductList(@PathVariable("pageIndex") int pageIndex,
                                        @PathVariable("productTypeName")String productTypeName){
        List<Product> list = productService.getProductList(pageIndex, productTypeName);
        return list;
    }

    /**
     * 增加
     * @param product
     * @return
     */
    @RequestMapping("/addProduct")
    public String addProduct(@RequestBody Product product){
        int i=productService.addProduct(product);
        if (i>0){
            return "增加成功";
        }else {
            return "增加失败";
        }
    }

    /**
     * 通过id查询对应数据
     * @param productId
     * @return
     */
    @RequestMapping("/getProductId/{productId}")
    public Product getProductId(@PathVariable("productId")int productId){
        Product productId1 = productService.getProductId(productId);
        return productId1;
    }

    /**
     * 修改
     * @param product
     * @return
     */
    @RequestMapping("/updateProduct")
    public String updateProduct(@RequestBody Product product){
        Product product1 = productService.getProductId((int) product.getProductId());
        if (product1 != null) {
            int i = productService.updateProduct(product);
            if (i>0){
                return "修改成功";
            }else {
                return "修改失败";
            }
        }else {
            return "没有修改";
        }
    }

    /**
     * 删除
     * @param productId
     * @return
     */
    @RequestMapping("/deleteProduct")
    public String deleteProduct(@PathVariable("productId")int productId){
        int i=productService.deleteProduct(productId);
        if (i>0){
            return "删除成功";
        }else {
            return "删除失败！";
        }
    }
//产品类型
    /**
     * 查询所有产品类型
     * @return
     */
    @RequestMapping("/getProductTypeList")
    public List<Producttype>getProductTypeList(){
        List<Producttype> list = productService.getProductTypeList();
        return list;
    }

}
